/**
 * Description:检索用户的操作权限
 * Company：山东三米信息技术有限公司
 * @author lld
 * @version1.0
 * */
package com.douyu.water.core.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.douyu.water.core.dao.MenuMapper;
import com.douyu.water.core.dao.RoleMenuMapper;
import com.douyu.water.core.dao.UserMapper;
import com.douyu.water.core.model.Menu;
import com.douyu.water.core.model.MenuExample;
import com.douyu.water.core.model.RoleMenuExample;
import com.douyu.water.core.model.RoleMenuKey;
import com.douyu.water.core.model.User;

public class UserUtil 
{
	@Resource
	private MenuMapper menuMapper;
	@Resource
	private RoleMenuMapper roleMenuMapper;
	@Resource
	private UserMapper userMapper;
	/**
	 * 根据角色选择用户的的操作权限
	 * @param menuId String
	 *        -菜单编号
	 * @param rolecode String
	 *        -角色编号
	 * @return List<Menu>
	 *        -检索结果
	 * */
   public List<Menu> queryOp(String menuId,User user)
   {
	   List<Menu> list = null;
	   List<RoleMenuKey> rlist = null;
	   RoleMenuExample example = new RoleMenuExample();
	   RoleMenuExample.Criteria criteria = example.createCriteria();
	   //获取用户编号
	   String code = user.getUcode();
	   if(code != null && code.trim().equals("1"))//超级管理员
	   {
		   MenuExample mexp = new MenuExample();
		   MenuExample.Criteria mc = mexp.createCriteria();
		   mc.andParentEqualTo(menuId.trim());
		   mc.andUtypeEqualTo("2");//仅检索操作功能
		   mexp.setOrderByClause(" isort asc");
		   list = menuMapper.selectByExample(mexp);
	   }
	   else
	   {
		   //获取用户角色
		   String rolecode = user.getRole();
		   rolecode = rolecode.replaceAll(" ", "");
		   criteria.andRolecodeEqualTo(rolecode.trim());
		   rlist = roleMenuMapper.selectByExample(example);
		   if(rlist != null && rlist.size() > 0)
		   {
			   List<String> menu = new ArrayList<String>();
			   for(int i = 0;i < rlist.size();i++)
			   {
				   menu.add(rlist.get(i).getMenucode().trim());
			   }
			   MenuExample mexp = new MenuExample();
			   MenuExample.Criteria mc = mexp.createCriteria();
			   mc.andUcodeIn(menu);
			   mc.andParentEqualTo(menuId.trim());
			   mc.andUtypeEqualTo("2");
			   mexp.setOrderByClause(" isort asc");
			   list = menuMapper.selectByExample(mexp);
		   }  
	   }	  	  
	   return list;
   }  
}
